﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Info
{
    public partial class EditChannel : PageBase
    {
        BLL.Channel bll = new BLL.Channel();
        Model.Channel model = null;

        string id = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Content", "modify");

            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Request.QueryString["id"];
            }
            if (!IsPostBack)
            {
                LoadList();
                ddlChannel.Items.Insert(0, new ListItem("请选择", "0"));
                GetData();
            }
        }

        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            DataView dv = new DataView(bll.GetList("parentID=" + id + " order by ChannelId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成下拉列表
                ListItem li = new ListItem(MakeFelgefu(count) + drv["ChannelName"].ToString(), drv["ChannelId"].ToString());
                ddlChannel.Items.Add(li);

                //再次遍历
                MakeTr(drv["ChannelId"].ToString(), count + 1);
            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }

        protected void GetData()
        {
            if (!string.IsNullOrEmpty(id))
            {
                model = bll.GetModel(int.Parse(id));
                txtTitle.Text = model.ChannelName;
                ddlChannel.SelectedValue = model.ParentId.ToString();
                ddlChannelType.SelectedValue = model.ChannelType.ToString();
                txtOrderNo.Text = Convert.ToString(model.OrderNo);
                FCKeditor1.Value = model.Detail;
            }
        }

        protected void BindData()
        {
            DataSet ds = bll.GetList("parentId=0");
            ddlChannel.DataSource = ds.Tables[0].DefaultView;
            ddlChannel.DataTextField = "channelName";
            ddlChannel.DataValueField = "channelId";
            ddlChannel.DataBind();

            ddlChannel.Items.Insert(0, new ListItem("请选择分类", "0"));
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(id))
            {
                model = bll.GetModel(int.Parse(id));
                model.ChannelName = txtTitle.Text.Trim();
                model.ParentId = int.Parse(ddlChannel.SelectedValue);
                model.ChannelType = int.Parse(ddlChannelType.SelectedValue);
                model.Detail = FCKeditor1.Value;
                model.Path = "";
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                bll.Update(model);

                MessageBox.ShowAndRedirect(this.Page, "修改成功", "Channel.aspx");
            }
            else
            {
                model = new Model.Channel();
                model.ChannelName = txtTitle.Text.Trim();
                model.ParentId = int.Parse(ddlChannel.SelectedValue);
                model.ChannelType = int.Parse(ddlChannelType.SelectedValue);
                model.Detail = FCKeditor1.Value;
                model.Path = "";
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                bll.Add(model);

                MessageBox.ShowAndRedirect(this.Page, "添加成功", "Channel.aspx");
            }
        }
    }
}